import re

def parse_entry(entry):
    # 提取子目录名
    dir_pattern = r'子目录名: (.+)'
    dir_match = re.search(dir_pattern, entry)
    if not dir_match:
        return None
    dir_name = dir_match.group(1)
    
    # 提取结果
    result = "-2"  # 默认无法求解
    if "规模有限，可以求解" in entry:
        result_pattern = r'结果是 (\d+)'
        result_match = re.search(result_pattern, entry)
        if result_match:
            result_value = result_match.group(1)
            result = result_value if result_value != "0" else "-1"
    
    return f"{dir_name},{result}\n"

def process_file(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 分割条目（假设每个条目以"======"分隔）
    entries = re.split(r'=+\n', content)
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write("子目录名,结果\n")  # 写入标题行
        for entry in entries:
            parsed = parse_entry(entry)
            if parsed:
                f.write(parsed)

# 使用示例
input_file = 'midresultbb.txt'  # 替换为你的输入文件名
output_file = 'bbtresult.txt'
process_file(input_file, output_file)
print(f"处理完成，结果已保存到 {output_file}")